Skip to content

[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916

Open
Dwrite wants to merge 1 commit intoapache:mainfrom
Dwrite:CALCITE-7475
Open

[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916
Dwrite wants to merge 1 commit intoapache:mainfrom
Dwrite:CALCITE-7475

Conversation

@Dwrite
Copy link
Copy Markdown

@Dwrite Dwrite commented May 4, 2026

Jira Link
CALCITE-7475

Changes Proposed
Summary
This PR fixes an issue in the Babel parser where postfix operators (such as array element access [] and field access .) were incorrectly handled or failed when following a PostgreSQL-style infix cast (::).

Problem
Previously, the parser could not clearly distinguish between a subscript that is part of a type definition (e.g., VARCHAR ARRAY) and a subscript that is an operator acting on the result of the cast. For example, in the expression 'test'::VARCHAR ARRAY[1], the [1] was often greedily or incorrectly consumed, leading to parsing errors or an invalid AST.

Solution
Eager Reduction in Parser: Modified the InfixCast logic to eagerly build the SqlCall for the :: operator. This ensures the cast is correctly scoped before handling any subsequent operators.

Postfix Expression Loop: Introduced a loop within the InfixCast rule to explicitly handle trailing (for ITEM access) and (for field access). This prevents these operators from being lost or incorrectly associated with the type specification.

Refined InfixCastDataType: Created a restricted variant of the data type parser specifically for infix casts. It correctly handles the ARRAY keyword while leaving subscript consumption to the expression layer.

Improved Unparsing: Updated the unparse logic for the :: operator to rely on operator precedence rather than forcing redundant parentheses, ensuring cleaner and more idiomatic PostgreSQL-style SQL output.

@xuzifu666
Copy link
Copy Markdown
Member

Your jira link is not right, please fix it first.

@Dwrite
Copy link
Copy Markdown
Author

Dwrite commented May 6, 2026

Your jira link is not right, please fix it first.

sorry,fixed

new SqlParserUtil.ToTreeListItem(SqlLibraryOperators.INFIX_CAST,
s.pos()));
list.add(dt);
SqlNode leftOperand = (SqlNode) list.remove(list.size() - 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the name of this production rule should be changed from InfixCast because now it does more. InfixCastOrFieldAccess?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current name InfixCast is more appropriate because the added logic is strictly to prevent the 'greedy' consumption of subscripts by the type specification. Since we are essentially fixing the precedence of the :: operator rather than introducing a standalone field access rule, keeping the original name ensures clarity for future maintenance.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label May 7, 2026
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants